package cup.com.icss.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cup.com.icss.dao.SomethingLostMapper;
import cup.com.icss.model.SomethingLost;
import cup.com.icss.model.User;
import cup.com.icss.utils.LogUtil;
import cup.com.icss.utils.StringUtil;

@Controller
public class somethingLostController {
	@Resource
	private SomethingLostMapper somethingLostMapper;

	@RequestMapping("/somethingLostAdd")
	@ResponseBody
	public ModelAndView somethingLostAdd(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("releasePeople",user.getCnName());
			view.addObject("college",user.getCollege());
			view.addObject("roleName",user.getRoleName());
			view.addObject("releasePeopleId",user.getStudentId());
			view.setViewName("cup/jsp/somethingLost/somethingLostAdd");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/somethingLostInster",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> somethingLostInster(HttpServletRequest request,SomethingLost somethingLost){
		Map<String, String> map = new HashMap<String, String>();
		try {
			Date date = new Date();
			somethingLost.setId(StringUtil.getUUID());
			somethingLost.setStatus("1");
			somethingLost.setCreateTime(date);
			int inster = somethingLostMapper.insertSelective(somethingLost);
			if(inster>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/GetComethingLostList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> GetComethingLostList(HttpServletRequest request,Integer page,Integer rows,SomethingLost record){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			if(page != null && rows != null){
				page=page==null?1:page;
				rows=rows==null?10:rows;
				int pageNum = page;
				int pageSize = rows;
				int num = somethingLostMapper.selectSomethingLostListSize(record).size();
				int pageSizez;
				if(((pageSize*pageNum)-num)<0){
					pageSizez = pageSize;
				}else{
					pageSizez = num-(10*(pageNum-1));
				}
				List<SomethingLost> somethingLost = somethingLostMapper.selectSomethingLostList(pageNum*pageSize-pageSize,pageSizez,record);
				map.put("total", num);
				map.put("rows", somethingLost);
			}else{
				int total = somethingLostMapper.selectSomethingLostListSize(record).size();
				List<SomethingLost> somethingLost = somethingLostMapper.selectSomethingLostList(0,6,record);
				map.put("total", total);
				map.put("rows", somethingLost);
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/comethingLostShow",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView comethingLostShow(HttpServletRequest request,String id){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			SomethingLost somethingLost = somethingLostMapper.selectByPrimaryKey(id);//
			view.addObject("roleName",user.getRoleName());
			view.addObject("studentId",user.getStudentId());
			view.addObject("college",somethingLost.getCollege());
			view.addObject("releasePeopleId",somethingLost.getReleasePeopleId());
			view.addObject("releasePeople",somethingLost.getReleasePeople());
			view.addObject("title",somethingLost.getTitle());
			view.addObject("content",somethingLost.getContent());
			view.addObject("status",somethingLost.getStatus());
			view.addObject("id",id);
			view.setViewName("cup/jsp/somethingLost/somethingLostShow");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/updaSomethingLostStatus",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> updaSomethingLostStatus(HttpServletRequest request,SomethingLost record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int updata = somethingLostMapper.updateByPrimaryKeySelective(record);
			if(updata>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/somethingLostListDatagrid")
	@ResponseBody
	public ModelAndView somethingLostListDatagrid(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("roleName",user.getRoleName());
			view.setViewName("cup/jsp/somethingLost/somethingLostList");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/somethingLostDelete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> somethingLostDelete(HttpServletRequest request,String id){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int delete = somethingLostMapper.deleteByPrimaryKey(id);
			if(delete>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
}
